package com.cap.dreamcircle.View.Adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cap.dreamcircle.App.App;
import com.cap.dreamcircle.Consts.Constants;
import com.cap.dreamcircle.Model.Action.DeleteFriendCircleAction;
import com.cap.dreamcircle.Model.Action.LikeDreamCircleAction;
import com.cap.dreamcircle.Model.Bean.DreamCircleUserRecordsEntity;
import com.cap.dreamcircle.R;
import com.cap.dreamcircle.Utils.DisplayUtil;
import com.cap.dreamcircle.Utils.KeyCounterUtils;
import com.cap.dreamcircle.Utils.MD5Utils;
import com.cap.dreamcircle.Utils.ShareDialogUtils;
import com.cap.dreamcircle.Utils.SimpleMediaPlayerUtils;
import com.cap.dreamcircle.Utils.TimeUtils;
import com.cap.dreamcircle.Utils.ViewHolderUtils;
import com.cap.dreamcircle.View.DreamDetailActivity;
import com.cap.dreamcircle.View.SolveDreamActivity;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by liucxu on 2017/9/8.
 */

public class PersonInfoDreamCircleAdapter extends BaseAdapter {

    private Context context;
    private List<DreamCircleUserRecordsEntity> datas = new ArrayList<>();

    public PersonInfoDreamCircleAdapter(Context c, List<DreamCircleUserRecordsEntity> sources) {
        if (sources != null) {
            datas.clear();
            datas.addAll(sources);
            context = c;
        }
    }

    public void setDatas(List<DreamCircleUserRecordsEntity> sources) {
        if (sources != null) {
            datas.clear();
            datas.addAll(sources);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.adapter_personal_info_dream_circle, null);
        }
        initViews(convertView, position);
        return convertView;
    }

    private void initViews(View convertView, int position) {
        final DreamCircleUserRecordsEntity entity = datas.get(position);
        TextView tv_dream_content = ViewHolderUtils.get(convertView, R.id.tv_dream_content);
        TextView tv_friend_dream_time = ViewHolderUtils.get(convertView, R.id.tv_friend_dream_time);
        ImageView imv_dream_delete = ViewHolderUtils.get(convertView, R.id.imv_dream_delete);
        ImageView imv_dream_like = ViewHolderUtils.get(convertView, R.id.imv_dream_like);
        ImageView imv_dream_comment = ViewHolderUtils.get(convertView, R.id.imv_dream_comment);
        TextView tv_like_num = ViewHolderUtils.get(convertView, R.id.tv_like_num);
        TextView tv_comment_num = ViewHolderUtils.get(convertView, R.id.tv_comment_num);
        RelativeLayout layout_audio = ViewHolderUtils.get(convertView, R.id.layout_audio);
        ImageView imv_dream_share = ViewHolderUtils.get(convertView, R.id.imv_dream_share);
        ImageView imv_dream_solve = ViewHolderUtils.get(convertView, R.id.imv_dream_solve);
        final ImageView imv_playing_status_audio=ViewHolderUtils.get(convertView,R.id.imv_playing_status_audio);
        tv_dream_content.setText(entity.getDreamText());
        tv_friend_dream_time.setText(TimeUtils.formatTime(entity.getCreateTime()));
        if (TextUtils.isEmpty(entity.getDreamVoiceUrl())) {
            layout_audio.setVisibility(View.GONE);
        } else {
            layout_audio.setVisibility(View.VISIBLE);
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(layout_audio.getLayoutParams());
            layoutParams.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
            int min_width = DisplayUtil.dip2px(App.GetInstance(), 128);
            int max_width = DisplayUtil.GetDisplayWidth() - DisplayUtil.dip2px(App.GetInstance(), 36);
            int width = (DisplayUtil.GetDisplayWidth() - DisplayUtil.dip2px(context, 30)) / 3;
            layout_audio.getLayoutParams().width = width;
            layout_audio.requestLayout();
            imv_playing_status_audio.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SimpleMediaPlayerUtils.getInstance(context).play(Constants.BASE_URL + entity.getDreamVoiceUrl(), new SimpleMediaPlayerUtils.VoicePlayInterface() {
                        @Override
                        public void onStartPlaying() {
                            imv_playing_status_audio.setImageResource(0);
                            imv_playing_status_audio.setImageDrawable(null);
                            imv_playing_status_audio.setImageURI(null);
                            imv_playing_status_audio.setImageResource(R.mipmap.audio_playing);
                        }

                        @Override
                        public void onPause() {
                            imv_playing_status_audio.setImageResource(0);
                            imv_playing_status_audio.setImageDrawable(null);
                            imv_playing_status_audio.setImageURI(null);
                            imv_playing_status_audio.setImageResource(R.mipmap.audio_pause);
                        }

                        @Override
                        public void onPlayCompleted() {
                            imv_playing_status_audio.setImageResource(0);
                            imv_playing_status_audio.setImageDrawable(null);
                            imv_playing_status_audio.setImageURI(null);
                            imv_playing_status_audio.setImageResource(R.mipmap.audio_pause);
                        }
                    });
                }
            });
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (entity.isPublic()) {
                    Intent intent = new Intent(context, DreamDetailActivity.class);
                    intent.putExtra("dreamId", entity.getDreamId());
                    context.startActivity(intent);
                }
            }
        });
        if (entity.isMineDreamCircleRecord()) {
            imv_dream_delete.setVisibility(View.VISIBLE);
            imv_dream_delete.setEnabled(true);
            imv_dream_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    KeyCounterUtils.SendKeyCounter("927fbdd9e9fc4f358bae23f48c155e62");
                    DeleteFriendCircleAction action = new DeleteFriendCircleAction();
                    action.setDreamId(entity.getDreamId());
                    EventBus.getDefault().post(action);
                }
            });
        } else {
            imv_dream_delete.setVisibility(View.INVISIBLE);
            imv_dream_delete.setEnabled(false);
        }
        if (entity.isPublic()) {
            imv_dream_like.setVisibility(View.VISIBLE);
            tv_like_num.setVisibility(View.VISIBLE);
            imv_dream_comment.setVisibility(View.VISIBLE);
            tv_comment_num.setVisibility(View.VISIBLE);
            if (entity.isLiked()) {
                imv_dream_like.setEnabled(false);
                imv_dream_like.setImageResource(R.mipmap.imv_dream_liked);
            } else {
                imv_dream_like.setEnabled(true);
                imv_dream_like.setImageResource(R.mipmap.imv_dream_like);
                imv_dream_like.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        LikeDreamCircleAction action = new LikeDreamCircleAction();
                        action.setDreamId(entity.getDreamId());
                        EventBus.getDefault().post(action);
                    }
                });
            }
            if (entity.getCommentCount() == 0) {
                tv_comment_num.setVisibility(View.GONE);
            } else {
                tv_comment_num.setVisibility(View.VISIBLE);
                tv_comment_num.setText(entity.getCommentCount() + "");
            }
            if (entity.getLikeCount() == 0) {
                tv_like_num.setVisibility(View.GONE);
            } else {
                tv_like_num.setVisibility(View.VISIBLE);
                tv_like_num.setText(entity.getLikeCount() + "");
            }
            if (entity.isMineDreamCircleRecord()) {
                imv_dream_share.setVisibility(View.VISIBLE);
//                imv_dream_share.setVisibility(View.GONE);
                imv_dream_share.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String url="http://app.5idreamer.com:8080/dream/"+"dream.do?task=shareDream&"+"dreamId="+entity.getDreamId()+"&validCode="+ MD5Utils.GetMD5Value(entity.getDreamId()+"dreamio");
                        ShareDialogUtils.init().share(context,url);
                    }
                });
            } else {
                imv_dream_share.setVisibility(View.GONE);
            }
        } else {
            imv_dream_share.setVisibility(View.GONE);
            imv_dream_like.setVisibility(View.GONE);
            tv_like_num.setVisibility(View.GONE);
            imv_dream_comment.setVisibility(View.GONE);
            tv_comment_num.setVisibility(View.GONE);
        }
        if (entity.isMineDreamCircleRecord()) {
            imv_dream_solve.setVisibility(View.VISIBLE);
            imv_dream_solve.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    KeyCounterUtils.SendKeyCounter("463b781908b3423582c570f4cbc8e388");
                    Intent intent=new Intent(context, SolveDreamActivity.class);
                    intent.putExtra("dreamDetail",entity.getDreamText());
                    intent.putExtra("dreamTime",entity.getCreateTime());
                    intent.putExtra("dreamVoiceUrl",entity.getDreamVoiceUrl());
                    intent.putExtra("dreamId",entity.getDreamId());
                    context.startActivity(intent);
                }
            });
        } else {
            imv_dream_solve.setVisibility(View.GONE);
        }
        imv_dream_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (entity.isPublic()) {
                    Intent intent = new Intent(context, DreamDetailActivity.class);
                    intent.putExtra("dreamId", entity.getDreamId());
                    context.startActivity(intent);
                }
            }
        });
    }


    /**
     * 将输入的流转换为byte数组
     * @param path 输入的流
     * @return byte数组
     */
    public static byte[] inputStreamToByte(String path)
    {
        try {
            FileInputStream fis = new FileInputStream(path);
            ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
            int ch;
            while ((ch = fis.read()) != -1) {
                bytestream.write(ch);
            }
            byte imgdata[] = bytestream.toByteArray();
            bytestream.close();
            return imgdata;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
